home *** CD-ROM | disk | FTP | other *** search
/ Winzipper / Winzipper_ISO.iso / programming / oracle7 7.2 / DB / UTIL72 / SAMPLE5.PC < prev    next >
Encoding:
Text File  |  1995-05-18  |  4.3 KB  |  145 lines

  1. #ifdef RCSID
  2. static char *RCSid = 
  3.    "$Header: sample5.pc.d,v 1.2 93/10/20 12:02:19 kosinski: Exp $ ";
  4. #endif /* RCSID */
  5.  
  6. /* Copyright (c) 1991 by Oracle Corporation */
  7. /*
  8.    NAME
  9.      sample5.pc - <one-line expansion of the name>
  10.    DESCRIPTION
  11.      <short description of component this file declares/defines>
  12.    PUBLIC FUNCTION(S)
  13.      <list of external functions declared/defined - with one-line descriptions>
  14.    PRIVATE FUNCTION(S)
  15.      <list of static functions defined in .c file - with one-line descriptions>
  16.    RETURNS
  17.      <function return values, for .c file with single function>
  18.    NOTES
  19.      <other useful comments, qualifications, etc.>
  20.    MODIFIED   (MM/DD/YY)
  21.     rvasired   05/12/92 -  Creation 
  22. */
  23. /*********************************************************************
  24.  *                                                                   *
  25.  *  EMBEDDED PL/SQL DEBIT TRANSACTION DEMO                           *
  26.  *                                                                   *
  27.  *  This program prompts the user for an account number and a debit  *
  28.  *  amount.  It makes sure that the account number is valid and that *
  29.  *  there are sufficient funds to cover the withdrawal before it     *
  30.  *  it debits the account.                                           *
  31.  *                                                                   *
  32.  *  Copyright (c) 1989,1992 by Oracle Corporation.                   *
  33.  *********************************************************************/
  34.  
  35. #include <stdio.h>
  36.  
  37.     char   buf[20];
  38.  
  39. EXEC SQL BEGIN DECLARE SECTION;
  40.     int      acct;
  41.     double   debit;
  42.     double   new_bal;
  43.     VARCHAR  status[65]; 
  44.     VARCHAR  uid[20];
  45.     VARCHAR  pwd[20];
  46. EXEC SQL END DECLARE SECTION;
  47.  
  48. EXEC SQL INCLUDE SQLCA;
  49.  
  50. main()
  51. {
  52.     extern double atof();
  53.  
  54.     strcpy (uid.arr,"scott");
  55.     uid.len=strlen(uid.arr);
  56.     strcpy (pwd.arr,"tiger");
  57.     pwd.len=strlen(pwd.arr);
  58.  
  59.     printf("\n\n\tEmbedded PL/SQL Debit Transaction Demo\n\n");
  60.     printf("Trying to connect...");
  61.  
  62.     EXEC SQL WHENEVER SQLERROR GOTO errprint;
  63.  
  64.     EXEC SQL CONNECT :uid IDENTIFIED BY :pwd;
  65.     printf(" connected.\n");
  66.  
  67.  
  68. for (;;)   /*  Loop infinitely */
  69.   {
  70.     printf("\n** Debit which account number? (-1 to end) ");
  71.     gets(buf);
  72.     acct = atoi(buf);
  73.     if (acct == -1)     /* Need to disconnect from ORACLE */
  74.     {                   /* and exit loop if account is -1 */
  75.         EXEC SQL COMMIT RELEASE;
  76.     exit(0);                 
  77.     }
  78.  
  79.     printf("   What is the debit amount? ");
  80.     gets(buf);
  81.     debit = atof(buf);
  82.  
  83.     /* ---------------------------------- */
  84.     /* ----- Begin the PL/SQL block ----- */
  85.     /* ---------------------------------- */
  86.     EXEC SQL EXECUTE
  87.  
  88.     DECLARE
  89.     insufficient_funds  EXCEPTION;
  90.     old_bal             NUMBER;
  91.     min_bal             CONSTANT NUMBER := 500;
  92.  
  93.     BEGIN
  94.         SELECT bal INTO old_bal FROM accounts
  95.           WHERE account_id = :acct;
  96.            -- If the account doesn't exist, the NO_DATA_FOUND
  97.            -- exception will be automatically raised.
  98.  
  99.         :new_bal := old_bal - :debit;
  100.  
  101.         IF :new_bal >= min_bal THEN
  102.             UPDATE accounts SET bal = :new_bal
  103.                WHERE account_id = :acct;
  104.             INSERT INTO journal
  105.                VALUES (:acct, 'Debit', :debit, SYSDATE);
  106.             :status := 'Transaction completed.';
  107.         ELSE 
  108.             RAISE insufficient_funds;
  109.         END IF;
  110.  
  111.     COMMIT;
  112.  
  113.     EXCEPTION
  114.     WHEN NO_DATA_FOUND THEN
  115.         :status := 'Account not found.';
  116.         :new_bal := -1;
  117.     WHEN insufficient_funds THEN 
  118.         :status := 'Insufficient funds.';
  119.         :new_bal := old_bal;
  120.         WHEN OTHERS THEN
  121.             ROLLBACK;
  122.             :status := 'Error: ' || SQLERRM(SQLCODE);
  123.             :new_bal := -1;
  124.     END;
  125.  
  126.     END-EXEC;
  127.     /* -------------------------------- */
  128.     /* ----- End the PL/SQL block ----- */
  129.     /* -------------------------------- */
  130.  
  131.     status.arr[status.len] = '\0';    /* null-terminate the string */
  132.  
  133.     printf("\n\n   Status:  %s\n", status.arr); 
  134.     if (new_bal >= 0)
  135.         printf("   Balance is now:  $%.2f\n", new_bal);
  136.   }  /* End of loop */
  137.  
  138. errprint:
  139.     EXEC SQL WHENEVER SQLERROR CONTINUE;
  140.     printf("\n\n>>>>> Error during execution:\n");
  141.     printf("%s\n",sqlca.sqlerrm.sqlerrmc);
  142.     EXEC SQL ROLLBACK RELEASE;
  143.     exit(1);
  144. }
  145.